package com.dd;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.dd.constant.Constant;
import com.dd.model.ConnectionInfo;
import com.dd.service.JdbcConnectionFactory;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.List;

public class DbDiffApplication extends Application {

    @Override
    public void start(Stage primaryStage) {
        try {
            ConnectionInfo testConnectionInfo = new ConnectionInfo("测试环境", "root",
                    "123456", "192.168.69.25:3306");
            ConnectionInfo prodConnectionInfo = new ConnectionInfo("生产环境", "soluxcrm",
                    "1qaz3EDC2wsx", "119.23.26.43:36167");
            JdbcConnectionFactory.CONNECTION_MAP.put(testConnectionInfo.getName(), testConnectionInfo);
            JdbcConnectionFactory.CONNECTION_MAP.put(prodConnectionInfo.getName(), prodConnectionInfo);
            JdbcConnectionFactory.sourceConnectionInfo = testConnectionInfo;
            JdbcConnectionFactory.targetConnectionInfo = prodConnectionInfo;

            // 新增：初始化JSON配置文件
            String jsonPath = "database-config.json";
            if (!FileUtil.exist(jsonPath)) {
                List<ConnectionInfo> defaultConnections = new ArrayList<>(JdbcConnectionFactory.CONNECTION_MAP.values());
                String json = JSONUtil.toJsonStr(defaultConnections);
                FileUtil.writeUtf8String(json, jsonPath);
                System.out.println("初始化默认数据库连接配置文件成功");
            }

            System.setProperty("javafx.font.family", "System");
            FXMLLoader loader = new FXMLLoader(DbDiffApplication.class.getResource("/fxml/Main.fxml"));
            BorderPane root = loader.load();

            root.lookupAll(".label")
                    .forEach(node -> node.setStyle("-fx-font-family: 'System';"));

            Scene scene = new Scene(root, 800, 600);
            primaryStage.setScene(scene);
            primaryStage.setTitle(Constant.APP_NAME + Constant.VERSION);
            Image icon = new Image(getClass().getResourceAsStream("/icons/icon.png"));
            primaryStage.getIcons()
                    .add(icon);
            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}